﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DTO;
using System.Xml;

namespace WebRaoVat.Admin
{
    public partial class QuanLyLogo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["LaAdmin"] == null || Session["LaAdmin"].ToString().Length <= 0)
            {                
                //Response.Redirect("../Default.aspx");
            }
        }

        protected void btnDongY_Click(object sender, EventArgs e)
        {
            string tenLogo = txtTenLogo.Text.Trim();

            if ("" == fileUpload.PostedFile.FileName || "" == tenLogo)
                return;
            string duongDan = Server.MapPath("~/images");
            string tenthat = Guid.NewGuid().ToString() + "." + fileUpload.PostedFile.FileName.Split('.')[1];
            duongDan += "/" + tenthat;
            fileUpload.PostedFile.SaveAs(duongDan);

            LogoDTO logo = new LogoDTO();
            logo.Delete = 1;

            int _count = duongDan.LastIndexOf("\\");
            logo.DuongDanLogo = duongDan.Remove(0, _count+1);
            logo.TenLogo = tenLogo;
            LogoBUS.Insert(logo);
            GridView1.DataBind();
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            LogoDTO logo = new LogoDTO();
            if (LogoBUS.getAll().Count > 0)
                logo = LogoBUS.getAll()[0];
            else
                logo.DuongDanLogo = "images/logo.gif";
            

            XmlDocument taiLieu = null;
            try
            {
                taiLieu = XL_XML.ReadDocumentFile(Server.MapPath("../Gui.xml"));
            }
            catch { }
            if (taiLieu == null)
                return;

            XL_XML.DeleteNode(taiLieu, "//GIAODIEN/LOGO");// xóa MENU cũ ra
            ThemLogo(taiLieu, logo);

            XL_XML.WriteDocument(taiLieu, Server.MapPath("../Gui.xml"));
        }

        public void ThemLogo(XmlDocument taiLieu, LogoDTO logo)
        {
            List<string> tenThuocTinh = new List<string>();
            List<string> giaTriThuocTinh = new List<string>();
            tenThuocTinh.Add("duongdan"); giaTriThuocTinh.Add(logo.DuongDanLogo);
            XmlNode Menu = XL_XML.CreateNode(taiLieu, "LOGO", tenThuocTinh, giaTriThuocTinh);
            XL_XML.AddNode(taiLieu, "//GIAODIEN", Menu);
        }

    }
}
